<template>
  <div class="app-container">
      <span>教练信息</span>
    <el-form label-width="120px">
      <el-form-item label="姓名"><el-input style="width: 200px"  v-model="coach.name"/></el-form-item>
      <el-form-item label="性别">
        <template>
          <el-select v-model="coach.sex" placeholder="请选择">
            <el-option :label="'男'" :value="0"></el-option>
            <el-option :label="'女'" :value="1"></el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="手机"><el-input style="width: 200px"  v-model="coach.tel"/></el-form-item>
      <el-form-item label="简介"><el-input style="width: 400px" type="textarea" :rows="5" v-model="coach.introduction"/></el-form-item>
      <el-form-item label="从业时间"><el-input style="width: 200px" v-model="coach.workingTime"/>年</el-form-item>
      <el-form-item label="所属场馆">
        <el-select v-model="coach.gymId">
          <el-option v-for="item in gym" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="擅长标签">
        <el-tag
          :key="tag"
          v-for="tag in dynamicTags"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)">
          {{tag}}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">添加标签</el-button>
      </el-form-item>

      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8080/upload/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="img" :src="img" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="edit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import coach from "../../../api/coach";
import gym from "../../../api/gym";
import goods from "../../../api/goods";
  export default {
    data(){
      return{
        coach:{
          sex: ""
        },
        saveBtnDisabled:false,
        dynamicTags: [], //标签数组
        inputVisible: false,
        inputValue: '', //输入的标签
        dialogVisible: false,
        img:"",
        gym:[]
      }
    },
    created() {
      if(this.$route.params!=null&&this.$route.params.id!=null){
        this.queryById(this.$route.params.id);
      }
      gym.queryAll().then(res=>{
        this.gym=res.data;
      })

    },
    methods: {
      edit(){
        if(this.$route.params && this.$route.params.id){
          coach.modify(this.coach,this.dynamicTags).then(res => {
            this.$message.success(res.message)
            this.$router.push({path: `/account/coach`})
          })
        }else {
          if(this.dynamicTags.length==0){
            let a = "该商品没有添加标签"
            this.dynamicTags.push(a)
          }
          coach.add(this.coach,this.dynamicTags).then(res=>{
            this.$message.success(res.message)
            this.$router.push({path:`/account/coach`})
          })
        }
      }
      ,
      queryById(id) {
        coach.getById(id).then(res => {
          this.coach = res.data.coach
          this.dynamicTags = res.data.dynamicTags
          console.log(this.dynamicTags)
          this.dynamicTags.forEach((item)=>{
            console.log(item)
            if(item=="该商品没有添加标签"){
              console.log(item)
              this.dynamicTags=[]
            }
          })
          this.img = "http://rj0387v46.hn-bkt.clouddn.com/" + res.data.coach.img;
        })
      },
      handleInputConfirm(){
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },
      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },
      handleAvatarSuccess(res, file) {
        this.img = "http://rj0387v46.hn-bkt.clouddn.com/" + res.data;
        this.coach.img = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
